package com.zy.system.exception;

import com.zy.common.result.Result;
import com.zy.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Author Gong
 * @Description 
 * @dATE 2023/6/5 8:39
 **/
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * @Author GongXL
     * @Description  全局异常处理
     * @Date 8:48 2023/6/5
     * @Param [java.lang.Exception]
     * @return com.zy.common.result.Result
     **/
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage());
    }
    /**
     * @Author GongXL
     * @Description  特定异常处理
     * @Date 8:48 2023/6/5
     * @Param [java.lang.ArithmeticException]
     * @return com.zy.common.result.Result
     **/
//    @ExceptionHandler(ArithmeticException.class)
//    @ResponseBody
//    public Result error(ArithmeticException e){
//        e.printStackTrace();
//        return Result.fail().message("执行了特定异常处理");
//    }
    /**
     * @Author GongXL
     * @Description  自定义异常
     * @Date 8:54 2023/6/5
     * @Param [com.zy.system.exception.ZyException]
     * @return com.zy.common.result.Result
     **/
    @ExceptionHandler(ZyException.class)
    @ResponseBody
    public Result error(ZyException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }

    /**
     *
     * @param e
     * @return 没有权限的异常
     * @throws AccessDeniedException
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail().code(ResultCodeEnum.PERMISSION.getCode()).message("没有当前功能的操作权限。");
    }
}